﻿using CustomCharacter;
using CustomItem;
using Exiled.Events.EventArgs.Map;
using Exiled.Events.EventArgs.Player;
using Exiled.Events.EventArgs.Scp914;
using InventorySystem.Items;
using InventorySystem.Items.Pickups;
using PlayerStatsSystem;
using RgCore.Events;
using UnityEngine;

namespace MiniMicroHID
{
    [ItemInfo("微型电磁炮", ItemType.MicroHID)]
    public class MiniMicroHID(ushort serial, bool active = true, ItemBase ib = null, ItemPickupBase ipb = null) : CustomItem.CustomItem(
        serial, active, ib, ipb)
    {
        public override void Item_Onactive()
        {
            base.Item_Onactive();
            Exiled.Events.Handlers.Map.PickupAdded += Map_PickupAdded;
            EventProcessor.OnHurting.Subscribe(Player_Hurting);
            Exiled.Events.Handlers.Scp914.UpgradingPickup += Scp914_UpgradingPickup;
            Exiled.Events.Handlers.Scp914.UpgradingInventoryItem += Scp914_UpgradingInventoryItem;
            Exiled.Events.Handlers.Player.ChangingMicroHIDState += Player_ChangingMicroHIDState;
        }


        public override void Item_Ondeactive()
        {
            base.Item_Ondeactive();
            Exiled.Events.Handlers.Map.PickupAdded -= Map_PickupAdded;
            EventProcessor.OnHurting.Unsubscribe(Player_Hurting);
            Exiled.Events.Handlers.Scp914.UpgradingPickup -= Scp914_UpgradingPickup;
            Exiled.Events.Handlers.Scp914.UpgradingInventoryItem -= Scp914_UpgradingInventoryItem;
            Exiled.Events.Handlers.Player.ChangingMicroHIDState -= Player_ChangingMicroHIDState;
        }
        private void Player_ChangingMicroHIDState(ChangingMicroHIDStateEventArgs ev)
        {
            if (ev.MicroHID.Serial != Serial || !ev.Player.GetCharacterInstance().Attribute.Name.Contains("SCP-2686"))
            {
                return;
            }
            if (ev.NewState == InventorySystem.Items.MicroHID.HidState.PoweringUp)
            {
                ev.NewState = InventorySystem.Items.MicroHID.HidState.Firing;
            }
        }
        private void Scp914_UpgradingInventoryItem(UpgradingInventoryItemEventArgs ev)
        {
            if (ev.Item.Serial != Serial)
            {
                return;
            }

            ev.IsAllowed = false;
        }

        private void Scp914_UpgradingPickup(UpgradingPickupEventArgs ev)
        {
            if (ev.Pickup.Serial != Serial)
            {
                return;
            }

            ev.IsAllowed = false;
        }

        private void Player_Hurting(HurtingEventArgs ev)
        {
            if (ev.DamageHandler.Base is AttackerDamageHandler && ev.Attacker?.CurrentItem != null &&
                ev.Attacker?.CurrentItem.Serial == Serial)
            {
                ev.Amount *= 0.1f;
                if (ev.Player.IsHuman)
                    ev.Amount *= 0.5f;
            }
        }

        private void Map_PickupAdded(PickupAddedEventArgs ev)
        {
            if (ev.Pickup.Serial != Serial)
            {
                return;
            }

            ev.Pickup.Scale = Vector3.one * 0.4f;
            ev.Pickup.Weight *= 0.25f;
        }
    }
}